/*
	Decimal Number Input plugin for jQuery
	Copyright (c) 2007-2009 Sebastian Arrubia (sarrubia.blogspot.com)

	Licensed under the MIT license
	Copyright (c) 2009 Sebastian Arrubia

    Permission is hereby granted, free of charge, to any
    person obtaining a copy of this software and associated
    documentation files (the "Software"), to deal in the
    Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the
    Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice
    shall be included in all copies or substantial portions of
    the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
    KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
    OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
    OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



    Version: 1.1.1 (06/09/2009)
*/
(function($) {

  $.fn.decimalNumber = function(settings) {
		settings = jQuery.extend({
		    name: "decimalNumber",
            decimals:2,
            decimalPoint: "."
		}, settings);

    return this.each(function() {
      
      $(this).keypress(function (e)
      {
          var lastvalue = $(this).val();
          //48-57 44=, 46=.
          var k = e.charCode || e.keyCode || e.which;

          if(k == 8 || k >= 37 && k <= 40 ) //Ignorar
              return true;
          
          if( (k >= 48 && k <= 57) || k == 44 || k == 46 )
          {
              if( settings.decimals > 0 )
              {
                  if( settings.decimalPoint == "," )
                  {
                        var xstr = '/^[0-9]*(?:,[0-9]{1,' + settings.decimals + '})?$/'
                        var xpre = new RegExp( eval(xstr) );

                        if( k == 44)
                            var str = lastvalue + String.fromCharCode(k)+'00';
                        else
                            var str = lastvalue + String.fromCharCode(k);
                  }
                  else
                  {
                        var xstr = '/^[0-9]*(?:\\.[0-9]{1,' + settings.decimals + '})?$/'
                        var xpre = new RegExp( eval(xstr) );

                        if( k == 46)
                            var str = lastvalue + String.fromCharCode(k)+'00';
                        else
                            var str = lastvalue + String.fromCharCode(k);
                  }
              }
              else
              {
                  var xpre = new RegExp( /^[0-9]*?$/ );
                  var str = lastvalue + String.fromCharCode(k);
              }

              if( str.match(xpre) )
                  return true;
              else
                  return false;
          }
          else
            return false;

      });

      function checkValue(e){
            var lastvalue = $(this).val();
            var k = e.charCode || e.keyCode || e.which;
            
            if( settings.decimals > 0 )
            {
              if( settings.decimalPoint == "," )
              {
                    var xstr = '/^[0-9]*(?:,[0-9]{1,' + settings.decimals + '})?$/'
                    var xpre = new RegExp( eval(xstr) );
              }
              else
              {
                    var xstr = '/^[0-9]*(?:\\.[0-9]{1,' + settings.decimals + '})?$/'
                    var xpre = new RegExp( eval(xstr) );
              }
            }
            else
            {
              var xpre = new RegExp( /^[0-9]*?$/ );
              var str = lastvalue + String.fromCharCode(k);
            }
              

              if( lastvalue.match(xpre) )
                  return true;
              else
                  $(this).val('');
      } 
      $(this).bind('blur',checkValue);
      $(this).bind('focus',checkValue);
      //$(this).blur().bind('focus');
      
    });
  };

})(jQuery);